// 作业： 回顾sql语句， 建立一个表(表的sql也要提交), 使用mysql模块实现增删改查(curd)
const mysql = require("mysql");
let connection = mysql.createConnection({
    host: 'localhost', //本地or主机名
    user: 'root', //用户名
    password: '123456', //密码
    database: 'demo' //数据库名称
});
//建立具体连接
connection.connect();


//增加一条数据
let sql1 = `INSERT INTO user ( username,password) VALUES ( '李四','147852')`;
connection.query(sql1, function(error, rows) {
    if (error) throw error;
    console.log(rows);

});

//修改
let sql2 = `UPDATE user SET username= '王五' ,password='123456' 
WHERE userid ='2'
`;
connection.query(sql2, function(error, rows) {
    if (error) throw error;
    console.log("修改成功");
});
//删除
let sql3 = `DELETE FROM user
WHERE username = '张三'`;
connection.query(sql3, function(error, rows) {
    if (error) throw error;
    console.log("删除成功");
});
//查询数据库
// 执行sql语句,查询数据表
connection.query('select * from user ', function(error, rows) {
    if (error) throw error;
    console.log(rows);

});
// 结束连接
connection.end();